package com.hdoit.kylin.web.groupActivity.controllers;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.poi.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.hdoit.kylin.logical.baseInfo.model.Term;
import com.hdoit.kylin.logical.baseInfo.service.ITermService;
import com.hdoit.kylin.logical.groupActivity.model.GroupDuty;
import com.hdoit.kylin.logical.groupActivity.service.IGroupDutyService;
import com.hdoit.kylin.logical.viewmodel.groupActivity.GroupDutyList;
import com.hdoit.kylin.logical.viewmodel.groupActivity.GroupDutySearch;
import com.hdoit.kylin.web.common.ScheduleUtils;
import com.hdoit.kylin.web.groupActivity.provider.GroupStudentProvider;
import com.hdoit.kylin.web.user.modelconfig.StudentConfig;
import com.hdoit.kylin.web.user.provider.StudentProvider;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

/**
 * 
 * @author ridfer
 *
 */

@Controller
@RequestMapping(value = "/admin/groupActivity/groupDuty/")
public class GroupDutyController extends BaseController {

	@Override
	public String getRoutePath() {
		return "areas/admin/groupActivity/groupDuty/";
	}

	private IGroupDutyService groupDutyService;
	private ITermService termService;

	public IGroupDutyService getGroupDutyService() {
		return groupDutyService;
	}

	@Resource
	public void setGroupDutyService(IGroupDutyService groupDutyService) {
		this.groupDutyService = groupDutyService;
	}

	public ITermService getTermService() {
		return termService;
	}

	@Resource
	public void setTermService(ITermService termService) {
		this.termService = termService;
	}

	private final String indexViewName = "index";
	private final String outputViewName = "output";
	private final String save = "save";
	private String termId;

	@RequestMapping(value = indexViewName, method = { RequestMethod.GET,
			RequestMethod.POST })
	public String index(Model model, GroupDutySearch search,
			PagerModel pagerModel, boolean ajaxPost) {
		List<GroupDuty> groupDuties = groupDutyService.queryList(search,
				null);
		System.err.println(groupDuties.size());
		Term term = termService.queryModel(search.getTermId());
		if (term == null) {
			term = termService.queryList().get(0);
		}
		termId = term.getTermId();
		term.generateDateList();
		if (groupDuties == null
				|| groupDuties.size() < term.getWeekDates().size()) {
			for (int i = groupDuties.size(); i < term.getWeekDates().size(); i++) {
				GroupDuty groupDuty = new GroupDuty();
				groupDuty.setWeekDate(term.getWeekDates().get(i));
				groupDuties.add(groupDuty);
			}
		}

		StudentProvider provider = new StudentProvider(
				StudentConfig.class.getName());

		model.addAttribute("groupDuties", groupDuties);
		model.addAttribute("groupDutySearch", search);
		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("employees",
				provider.getDataList(view(indexViewName)));

		if(ajaxPost){
			return view(outputViewName);
		}
		return view(indexViewName);
	}

	@RequestMapping(value = save, method = { RequestMethod.GET,
			RequestMethod.POST })
	public String save(Model model, GroupDutyList groupDutyList,
			GroupDutySearch search, PagerModel pagerModel) {
		System.err.println("saveGroupDuties");
		int i=0;
		Term term = termService.queryModel(termId);
		term.generateDateList();
		for (GroupDuty groupDuty : groupDutyList.getGroupDuties()) {
			if (StringUtils.isEmpty(groupDuty.getId())) {
				groupDuty.setId(CommonUtils.getKeyId());
				groupDuty.setModelStatus(ModelStatus.add);
				groupDuty.setTermId(termId);
				groupDuty.setWeekDate(term.getWeekDates().get(i));
				i++;
			} else {
				groupDuty.setModelStatus(ModelStatus.update);
			}
		}

		ActionResult actionResult;
		if (!groupDutyService.applyChanges(groupDutyList.getGroupDuties())) {
			actionResult = new ErrorResult();
		} else {
			actionResult = new ActionResult();
		}
		StudentProvider provider = new StudentProvider(
				StudentConfig.class.getName());
		
		model.addAttribute("groupDuties", groupDutyList.getGroupDuties());
		model.addAttribute(ManageContainer.RESULTKEY, actionResult);
		model.addAttribute("groupDutySearch", search);
		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("employees",
				provider.getDataList(view(indexViewName)));

		return view(indexViewName);
	}

}
